using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Xml.Linq;

namespace Bing
{
    /// <summary>
    /// This class contains: <br />
    /// <ul>
    /// <li>An array of advertisements returned for a request to the Ad <see cref="ApiHelper.SourceType"/></li>
    /// <li>Properties indicating the total number of advertisements returned, and the page number on which the ads appear</li>
    /// <li>Property indicating the version of the Bing API that processed the request.</li>
    /// </ul>
    /// </summary>
    [DebuggerDisplay("Total: {Total}, Version:{AdAPIVersion}, Count={Results.Count}, Errors={Errors.Count}")]
    public class AdResponse: SearchResponseBase
    {
        /// <summary>
        /// Initializes a new instance of the <see cref="AdResponse"/> class.
        /// </summary>
        public AdResponse(XContainer element):base (element)
        {
            Results = new List<AdResult>();
        }

        /// <summary>
        /// Gets or sets the total number of advertisements returned for a request..
        /// </summary>
        /// <value>Total number of advertisements returned for a request.</value>
        public UInt32 Total { get; set; }

        /// <summary>
        /// Gets or sets the page number on which advertisements in this set of results will appear.
        /// </summary>
        /// <value>Page number on which advertisements in this set of results will appear.</value>
        public UInt32 PageNumber { get; set; }

        /// <summary>
        /// Gets or sets the version of the Ad API that returns a response.
        /// </summary>
        /// <value>Version of the Ad API that returns a response.</value>
        public string AdAPIVersion { get; set; }

        /// <summary>
        /// Gets or sets the array of results returned for this <see cref="AdRequest"/>.
        /// </summary>
        /// <value>The results.</value>
        public List<AdResult> Results { get; set; }

        /// <summary>
        /// Parses the response from the Bing API.
        /// </summary>
        /// <param name="element">The XML from the Bing API</param>
        /// <returns>An <see cref="AdResponse" /></returns>
        public static AdResponse ParseElement(XContainer element)
        {
            AdResponse adResponse = new AdResponse(element);
            if (adResponse.Errors.Count > 0) return adResponse;

            var ad = element.Element(XName.Get("Ad", ApiHelper.AdsNs));
            if (ad == null) return adResponse;

            adResponse.Total = ApiHelper.GetElementValueOrDefault<UInt32>(ad, "Total", ApiHelper.AdsNs);
            adResponse.PageNumber = ApiHelper.GetElementValueOrDefault<UInt32>(ad, "Offset", ApiHelper.AdsNs);
            adResponse.AdAPIVersion = ApiHelper.GetElementValueOrDefault<string>(ad, "AdApiVersion", ApiHelper.AdsNs);

            XElement adResults = ad.Element(XName.Get("Results", ApiHelper.AdsNs));

            if (adResults == null) return adResponse;
            foreach (XElement result in adResults.Elements(XName.Get("AdResult", ApiHelper.AdsNs)))
            {
                AdResult adResult = new AdResult();
                adResult.AdlinkURL = ApiHelper.GetElementValueOrDefault<string>(result, "AdlinkUrl", ApiHelper.AdsNs);
                adResult.Rank = ApiHelper.GetElementValueOrDefault<UInt32>(result, "Rank", ApiHelper.AdsNs);
                adResult.Position = ApiHelper.GetElementValueOrDefault<string>(result, "Position", ApiHelper.AdsNs);
                adResult.Title = ApiHelper.GetElementValueOrDefault<string>(result, "Title", ApiHelper.AdsNs);
                adResult.Description = ApiHelper.GetElementValueOrDefault<string>(result, "Description", ApiHelper.AdsNs);
                adResult.DisplayURL = ApiHelper.GetElementValueOrDefault<string>(result, "DisplayUrl", ApiHelper.AdsNs);
                
                adResponse.Results.Add(adResult);
            }

            return adResponse;
        }

    }
}
